package commons.base.serialization.protostuff.example;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import commons.base.serialization.protostuff.Protostuff;
import io.protostuff.runtime.Field;
import io.protostuff.runtime.RuntimeSchema;

public class ProtostuffDemo {

	public static void main(String[] args)throws Exception {
		t6();
	}
	
	public static void t1()throws Exception{
		Person person1 = new Person();
		person1.id = 10086;
		person1.name = "ken";
		person1.email = "ken@iamcoding.com";
		person1.age = 10;
		
		person1.student = new Student("yuan@aliyun.com", 20);
		person1.list.add(new Student("yuan2@aliyun.com", 30));
		for(int i=0; i<2000; i++){
			person1.list.add(new Student("yuan3@aliyun.com", 40));
		}
		person1.map.put("1", new Student("yuan4@aliyun.com", 400));
		person1.map.put("2", new Student("yuan5@aliyun.com", 500));
		
		// 序列化成protobuf的二进制数据
		byte[] data = Protostuff.serial(person1);
		
		Files.write(Paths.get("/data/Persion.data"), data);
	}
	
	public static void t2()throws Exception{
		// 反序列化
		Person person2 = Protostuff.deserial(Files.readAllBytes(Paths.get("/data/Persion.data")), Person.class);
		System.out.println(person2.id);
		System.out.println(person2.name);
		System.out.println(person2.email);
		System.out.println(person2.age);
		System.out.println(person2.student.getAge());
		System.out.println(person2.student.getEmail());
		System.out.println("***" + person2.list.size());
		System.out.println("***" + person2.list.get(0).getAge());
		System.out.println("***" + person2.list.get(0).getEmail());
		System.out.println("===" + person2.map.get("1").getAge());
		System.out.println("===" + person2.map.get("1").getEmail());
	}
	
	public static void t3(){
		RuntimeSchema<Person> s1 = (RuntimeSchema<Person>)RuntimeSchema.getSchema(Person.class);
		System.err.println(s1.getFieldCount());
		for(Field<Person> f : s1.getFields()){
			System.err.println(f.name + ":" + f.number);
		}
	}
	
	public static void t4(){
//		UserEntity userEntity = new UserEntity();
//		userEntity.setCaptainName("111");
//		userEntity.setCreateTime(123456L);
//		userEntity.setId(123L);
//		userEntity.setLastLoginTime(123456L);
//		userEntity.setLoginType(1);
//		userEntity.setUsername("yuan");
//		
//		byte[] data = Protostuff.serial(userEntity);
//		System.err.println(data.length);
//		
//		UserEntity u2 = Protostuff.deserial(data, UserEntity.class);
//		System.err.println(u2.getUsername());
//		System.err.println(u2.getId());
	}
	
	public static void t5()throws Exception{
		Path path = Paths.get(ProtostuffDemo.class.getResource("/chip.bytes").toURI());
		
		byte[] data = Files.readAllBytes(path);
		ChipTempList chipList = Protostuff.deserialNoCompress(data, ChipTempList.class);
		System.err.println(chipList.getTemp());
		
	}
	
	public static void genUsersData() throws IOException{
		Users users = new Users();
		
		for(int i=0; i<5000; i++){
			Users.User user = new Users.User();
			user.setNick("hz0799");
			user.setSex("m");
			user.setAvatar("http://assets.taobaocdn.com/app/sns/img/default/avatar-120.png");
			users.getUserList().add(user);
		}
		
//		byte[] data = Protostuff.serialNoCompress(users);
		byte[] data = Protostuff.serial(users);
		Files.write(Paths.get("/data/user.data"), data);
	}
	
	public static void t6()throws Exception{
		long start = System.currentTimeMillis();
		
		Path path = Paths.get(ProtostuffDemo.class.getResource("/user.data").toURI());
		byte[] data = Files.readAllBytes(path);
		
		
//		Users users = Protostuff.deserialNoCompress(data, Users.class);
		Users users = Protostuff.deserial(data, Users.class);
		
		long end = System.currentTimeMillis();
		System.err.println("t1 : " + (end -start));
		System.err.println(users.getUserList().size());
	}
	
}
